import Vue from 'vue'
import Router from 'vue-router'
import store from '@/vuex/store'

Vue.use(Router)

const Layout = resolve => require(['@/templates/layout.vue'], resolve)

const router = new Router({
  mode: 'history',
  routes: [
    {
      path: '/',
      redirect: '/index'
    },
    {
      path: '/login',
      name: 'login',
      meta: {
        title: '登录'
      },
      component: resolve => require(['@/views/login.vue'], resolve)
    },
    {
      path: '/index',
      component: Layout,
      children: [
        {
          path: '',
          meta: {
            title: '首页'
          },
          component: resolve => require(['@/views/index/index.vue'], resolve)
        },

      ]
    },   
    {
      path: '/manage',
      component: Layout,
      children: [
        {
          path: 'list',
          name: 'list',
          meta: {
            title: '添加线索'
          },
          component: resolve => require(['@/views/manage/list.vue'], resolve)
        }, {
          path: 'share',
          name: 'share',
          meta: {
            title: '添加线索'
          },
          component: resolve => require(['@/views/manage/share.vue'], resolve)
        },{
          path: 'public',
          name: 'public',
          meta: {
            title: '添加线索'
          },
          component: resolve => require(['@/views/manage/public.vue'], resolve)
        }, {
          path: 'log',
          name: 'log',
          meta: {
            title: '添加线索'
          },
          component: resolve => require(['@/views/manage/log.vue'], resolve)
        }, {
          path: 'add',
          name: 'add',
          meta: {
            title: '添加线索'
          },
          component: resolve => require(['@/views/manage/add.vue'], resolve)
        },        
      ]
    },    
    {
      path: '/config',
      component: Layout,
      children: [
        {
          path: 'api',
          name: 'api',
          meta: {
            title: '添加线索'
          },
          component: resolve => require(['@/views/config/api.vue'], resolve)
        },{
          path: 'admin',
          name: 'admin',
          meta: {
            title: '添加线索'
          },
          component: resolve => require(['@/views/config/admin.vue'], resolve)
        },
      ]
    },
    {
      path: '/*',
      component: Layout,
      children: [
        {
          path: '',
          meta: {
            title: '404-页面不存在'
          },
          component: resolve => require(['@/views/error/404.vue'], resolve)
        }
      ]
    }
  ]
})

router.beforeEach((to, from, next) => {
  document.title = to.meta.title
  // if (store.getters.userInfo.token) {
  //   if (to.path == '/login') {
  //     next({ path: '/index' })
  //   }
  //   else {
  //     next()
  //   }
  // }
  // else {
    // if (to.path !== '/login') {
    //   // next({ path: '/login', query: { redirect: to.fullPath } })
    //   next({ path: '/login' })
    // }
    // else {
      store.commit('breadData', {})
      next()
    // }
  // }
})

router.afterEach((to, from) => {
  window.scrollTo(0, 0)
  if (to.path.indexOf('/system') > -1) {
    store.state.common.isSystem = true
  }
  else {
    store.state.common.isSystem = false
  }
})

export default router